local fuyuan = fk.CreateSkill{
    name = "tyj_heg__fuyuan",
}
local H = require "packages.ol_hegemony.util"

Fk:loadTranslationTable{
      ["tyj_heg__fuyuan"]="扶援",
      [":tyj_heg__fuyuan"]="当你的上下家成为【杀】的目标后，你可以交给其一张手牌。结束阶段，你可以将一张牌当【远交近攻】对一名没有手牌的角色使用。",
      ["#tyj_heg__fuyuan_slash-invoke"]="是否发动 扶援 交给 %dest 一张牌？",
      ["#tyj_heg__fuyuan_end-invoke"]="扶援：将一张牌当【远交近攻】对一名没有手牌的角色使用",
      ["#tyj_heg__fuyuan_prohibit"]="扶援 禁止出牌",
      ["#tyj_heg__fuyuan_active"] = "扶援",
      ["$tyj_heg__fuyuan1"]="今君困顿，扶援相助。",
      ["$tyj_heg__fuyuan2"]="恤君之患，以相扶援。",
    }

fuyuan:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(fuyuan.name) and player.phase == Player.Finish
    and not player:isNude() and #table.filter(player.room.alive_players, function(p) return p:isKongcheng() and not H.compareKingdomWith(player,p) end)>0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local success, dat = room:askToUseActiveSkill(player,{
      skill_name = "#tyj_heg__fuyuan_active",
      prompt = "#tyj_heg__fuyuan_end-invoke",
      extra_data = {
      skillName = fuyuan.name,
      cancelable = true,
    }})
    if success and dat then
      event:setCostData(self, { tos = dat.targets, cards = dat.cards })
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cards = event:getCostData(self).cards
    if player.dead or to.dead then return end
    room:useVirtualCard("befriend_attacking", cards, player, to, fuyuan.name, true)
  end,
})
fuyuan:addEffect(fk.TargetConfirmed, {
    anim_type = "control",
    can_trigger = function (self, event, target, player, data)
        if player:hasSkill(fuyuan.name)then
            return data.card and data.card.trueName == "slash" and (target:getNextAlive() == player or player:getNextAlive() == target) and not player:isKongcheng()
        end
      end,
      on_cost = function (self, event, target, player, data)
        local room = player.room
        local cards = room:askToCards(player, {
      skill_name = fuyuan.name,
      min_num = 1,
      max_num = 1,
      prompt = "#tyj_heg__fuyuan_slash-invoke::"..target.id,
      include_equip = false,
      cancelable = true,
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
      end,
      on_use = function (self, event, target, player, data)
        local room = player.room
        local cards = event:getCostData(self).cards
          room:doIndicate(player.id,{target.id})
          room:moveCardTo(cards, Card.PlayerHand, target, fk.ReasonGive, self.name,nil,false,player.id,nil,{player.id,target.id})
      end,
})
fuyuan:addEffect("prohibit",{
      is_prohibited = function(self, from, to, card)
        return table.contains(card.skillNames, fuyuan.name) and not to:isKongcheng()
      end,
})
    return fuyuan